资料
Demo
使用步骤
- 在指定的包上右击,弹出菜单里,批到新建向导
- 输入新建类名的前缀
- 查看向导生成的类文件
创建步骤
在 android studio 安装目录找到文件夹:{Android Studio installation dir}\plugins\android\lib\templates\other
依次创建以下文件:
template.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19<template
format="4"
revision="1"
name="Rx MPV"
description="Creates new RxMVP classes - Presenter, Model, Bean and Contract">
<category value="Other"/>
<parameter
id="prefix"
name="Prefix of Class Name"
type="string"
constraints="class|unique|nonempty"
default="Mvp"
help="The prefix of class name"/>
<globals file="globals.xml.ftl" />
<execute file="recipe.xml.ftl" />
</template>recipe.xml.ftl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<?xml version="1.0"?>
<recipe>
<instantiate
from="src/app_package/Contract.java.ftl"
to="${escapeXmlAttribute(srcOut)}/${prefix}Contract.java" />
<instantiate
from="src/app_package/Presenter.java.ftl"
to="${escapeXmlAttribute(srcOut)}/${prefix}Presenter.java" />
<instantiate
from="src/app_package/Model.java.ftl"
to="${escapeXmlAttribute(srcOut)}/${prefix}Model.java" />
<instantiate
from="src/app_package/Bean.java.ftl"
to="${escapeXmlAttribute(srcOut)}/${prefix}Bean.java" />
<open file="${srcOut}/${prefix}Contract.java"/>
</recipe>globals.xml.ftl
1
2
3
4
5<?xml version="1.0"?>
<globals>
<global id="resOut" value="${resDir}" />
<global id="srcOut" value="${srcDir}/${slashedPackageName(packageName)}" />
</globals>root/src/app_package/Contract.java.ftl
1
2
3
4
5
6
7
8
9
10
11
12package ${packageName};
public interface ${prefix}Contract{
interface Model {
}
interface View {
}
interface Presenter {
}
}root/src/app_package/Model.java.ftl
1
2
3
4package ${packageName};
public class ${prefix}Model implements ${prefix}Contract.Model {
}root/src/app_package/Presenster.java.ftl
${packageName}; 1
2
3
4
5
6
7
import work.wangxiang.android.common.PresenterBase;
public class ${prefix}Presenter
extends PresenterBase<${prefix}Contract.Model, ${prefix}Contract.View>
implements ${prefix}Contract.Presenter {
}root/src/app_package/Bean.java.ftl
1
2
3
4package ${packageName};
public class ${prefix}Bean {
}
创建完成后,重新启动 android studio,就可使用新建向导了。
遇到的问题
- template.xml 不要加 .ftl 后缀
- app_package 目录名中,是下划线,不是中划线